package com.xiaokang.utils;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 
 * @Description: 数据操作工具类
 * @author 小康
 * @version V1.0.0 2019年11月10日 上午8:56:48
 */
public abstract class DBUtils {
	private static Properties prop = null;
	static {
		prop = new Properties();
		try {
			prop.load(DBUtils.class.getClassLoader().getResourceAsStream("db.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private static final String DRIVER = prop.getProperty("driver");
	private static final String URL = prop.getProperty("url");
	private static final String UNME = prop.getProperty("uname");
	private static final String PWD = prop.getProperty("pwd");

	/**
	 * 
	 * @Description: 获取Connection对象
	 * @return Connection
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL, UNME, PWD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 
	 * @Description: 获得PreparedStatement预编译对象
	 * @return PreparedStatement
	 * @param sql
	 * @return
	 */
	public static PreparedStatement getPreparedStatement(String sql) {
		PreparedStatement pst = null;
		try {
			pst = getConnection().prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pst;
	}

	/**
	 * 
	 * @Description: 增删改
	 * @return int
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public static int modify(String sql, Object[] params) throws SQLException {
		int affectRows = -1;
		PreparedStatement pst = getPreparedStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				pst.setObject(i + 1, params[i]);
			}
		}
		affectRows = pst.executeUpdate();
		return affectRows;
	}

	/**
	 * 
	 * @Description: 查询
	 * @return ResultSet
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public static ResultSet query(String sql) throws SQLException {
		ResultSet rs = null;
		PreparedStatement pst = getPreparedStatement(sql);
		rs = pst.executeQuery();
		return rs;
	}

	/**
	 * 
	 * @Description: 查询（带有占位符）
	 * @return ResultSet
	 * @param sql
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public static ResultSet query(String sql, Object[] params) throws SQLException {
		ResultSet rs = null;
		PreparedStatement pst = getPreparedStatement(sql);
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				pst.setObject(i + 1, params[i]);
			}
		}
		rs = pst.executeQuery();
		return rs;
	}

	/**
	 * 
	 * @Description: 关闭连接，释放资源
	 * @return void
	 * @param conn
	 * @param pst
	 * @param rs
	 */
	public static void closeAll(Connection conn, PreparedStatement pst, ResultSet rs) {
		if (conn != null) {// 防止空指针异常
			try {
				conn.close();// 释放资源
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pst != null) {// 防止空指针异常
			try {
				pst.close();// 释放资源
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (rs != null) {// 防止空指针异常
			try {
				rs.close();// 释放资源
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @Description: 关闭连接，释放资源（无结果集）
	 * @return void
	 * @param conn
	 * @param pst
	 */
	public static void close(Connection conn, PreparedStatement pst) {
		if (conn != null) {// 防止空指针异常
			try {
				conn.close();// 释放资源
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pst != null) {// 防止空指针异常
			try {
				pst.close();// 释放资源
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @Description: 关闭连接，释放资源
	 * @return void
	 * @param conn
	 * @param pst
	 */
	public static void close(ResultSet rs) {
		if (rs != null) {// 防止空指针异常
			try {
				rs.close();// 释放资源
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
